package oldFiles;
import lejos.nxt.LightSensor;
import lejos.nxt.SensorPort;

public class DriveLight extends BehaviorOld 
{
    private static int lightLowThreshold = 560;
    private static int baseDriveSpeed = 80;
    private static int driveAndTurnSpeed = baseDriveSpeed + 20;

    LightSensor rightLight = new LightSensor(SensorPort.S3, true);
	LightSensor leftLight = new LightSensor(SensorPort.S4, true);

	public DriveLight( String name, int LCDrow, BehaviorOld b)
    {
    	super(name, LCDrow, b);
    }
    	
    public void run()
    {   
    	int leftRead, rightRead;
    	
    	while (true)
    	{ 
    	   leftRead = leftLight.readNormalizedValue();
     	   rightRead = rightLight.readNormalizedValue();
    	   
 		   /*if light is read above the threshold suppress lower priority 
 		    * behaviours and drive toward the light*/
     	   if (leftRead > lightLowThreshold || rightRead > lightLowThreshold) {
     		   suppress(); 
     		   /*keep driving toward the light as long as the light is above the threshold*/
     		   while (leftRead > lightLowThreshold || rightRead > lightLowThreshold)
     		   {
		       	   if (leftRead>rightRead) {
		       		   drawString("l");
		       		   forward(baseDriveSpeed,driveAndTurnSpeed); // turn left
		       	   }
		       	   else {
		       		   drawString("r");
            		   forward(driveAndTurnSpeed,baseDriveSpeed); // turn right
		       	   }
		       	   drawString("s");

		       	   leftRead = leftLight.readNormalizedValue();
		     	   rightRead = rightLight.readNormalizedValue();
     		   }
               /* cannot see light above the threshold - stop and release so that
                * another behaviour can take over*/
     		   stop();
	       	   release();
     	   }
     	   
    	}
    }
}
